package com.controller.admin;


import com.alibaba.fastjson.JSON;
import com.business.bean.User;
import com.business.bean.layuiBean.ResultPage;
import com.business.bean.layuiBean.VoPage;
import com.business.service.UserService;
import com.business.util.cacheUtil.JedisUtil;
import com.controller.annotation.LogMenthodName;
import com.controller.commen.BaseController;
import com.util.Respons.ResponseUtil;
import com.util.exception.CommonException;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.web.servlet.ShiroHttpSession;
import org.crazycake.shiro.RedisManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2019-01-08
 */
@Controller
@RequestMapping("/user")
@Api(description = "用户类 ", tags = "/user")
public class UserController extends BaseController {

    @Autowired
    UserService userService;
    @Autowired
    JedisUtil jedisUtil;
    @Autowired
    RedisManager redisManager;



    @Autowired
    RedisSessionDAO redisSessionDAO;
    /**
     * 用户管理主页面
     * @return
     */
    @RequestMapping(value = "/main.html", method = RequestMethod.GET)
    public String main() {
        return "system/user";
    }

    /**
     * 添加用户页面
     * @return
     */
    @RequiresPermissions("button:用户管理添加")
    @RequestMapping(value = "/userAdd.html", method = RequestMethod.GET)
    public String userAdd() {
        return "system/user_add";
    }



    /**
     *
     * @return
     */
    @RequestMapping(value = "/aa", method = RequestMethod.GET)
    @ResponseBody
    public List<User> aa() {
       // Cache cache=new RedisCache();
/*        Cache cache=new RedisCache(redisManager);
        Set<byte[]> set= cache.keys();
        for (byte[] s:set){
           System.err.println(new String(s));
        }*/
        jedisUtil.delSessionCache("admin");
        return    jedisUtil.getAllSession();
    }




    @RequestMapping(value = "/login.html", method = RequestMethod.GET)
    @ApiOperation(value = "登陆页面 ")
    @ApiResponses({@ApiResponse(code = 500, message = "服务器错误")})
    public Object login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //判断如果是ajax请求 来到了这里 说明session过期 或者是 当前登陆的用户已失效
        if(isAjaxRequest(request)){
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(JSON.toJSONString(ResponseUtil.getLoginAgainResponseMap()));//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        return    null;
        }else{
           return  "login";
        }

    }

    @LogMenthodName(name = "登陆日志")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用户登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户账号", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "passWord", value = "用户密码", dataType = "String", required = true, paramType = "query")
    })
    @ApiResponses({@ApiResponse(code = 500, message = "服务器错误")})
    public Map<String, Object> doLogin(String userName, String passWord ) {
        return userService.loginGo(userName, passWord);
    }





    @RequestMapping(value = "/index.html", method = RequestMethod.GET)
    @ApiOperation(value = "页面，shiro菜单 是标签控制显示与否")
    @ApiResponses({@ApiResponse(code = 500, message = "服务器错误")})
    public String index() {
        //  System.err.println(getUser().toString());
        return "index";
    }




    @RequestMapping(value = "/findUser", method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<User> findUser(VoPage voPage) {
        return  userService.findUserByPage(voPage);
    }


    @LogMenthodName(name = "添加用户AOP")
    @RequestMapping(value = "/insertUser", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("button:用户管理添加")
    public Map<String, Object> insertUser(User user,Long roleId) {
        return  userService.insertUser(user,roleId);
    }


    @LogMenthodName(name = "删除用户AOP")
    @RequestMapping(value = "/delUser", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("button:用户管理删除")
    public Map<String, Object> delUser(Long userId   ,String userName,ShiroHttpSession shiroHttpSession) {
        return  userService.delUser(userId,userName,shiroHttpSession);
    }





    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ApiOperation(value = "清除权限缓存测试")
    @ApiResponses({@ApiResponse(code = 500, message = "服务器错误")})
    @ResponseBody
    // @RequiresPermissions("menu:房态管理") // 菜单权限
    @RequiresRoles("role:超级管理员")  // 角色权限     shiroRealm中控制
    public String test() {
        //查看源码得知 插件固定缓存key为：
      //  jedis.del("shiro_redis_cache:admin");
        throw new CommonException("不知道的异常");
    }





}
